<template>
  <div class="app-container" style="padding:0">
    <div class="table_base_bt">
      <el-button class="filter-btn-item same-contrast" v-if="hasRole('all:send:contrast')"
                size="mini"
                type="green"
                @click="sameContrast">
        同样对比
      </el-button>
    </div>
    <drug-table :backCount="backCount"   @getSelection="getSelection" :isMultipleSelection="true"   @getBackData="getList" :tableData="tableData" :tableLoading="tableLoading" :tableHeader="tableHeader" :option="option">
      <template slot-scope="props" slot="operate">
        <div >
          <label @click="handleEditData(props.rowData)"  v-if="hasRole('all:send:see')"
                 class="table-view" style="cursor: pointer;">
            查看
          </label>
          <label  v-if="props.rowData.checkStatus == '5' && hasRole('all:send:report')" @click="generateRecord(props.rowData)"
                 class="table-view" style="cursor: pointer;">
            · &nbsp;报告
          </label>
        </div>
      </template>
    </drug-table>
  </div>
</template>

<script>
import waves from '@/views/directive/waves' // waves directive
import { formatDate } from '@/utils/formatDate'
import drugTable from "@/components/table/index";
import {getToken} from '@/utils/auth' // 验权
export default {
  components: { drugTable},
  directives: { waves },
  data() {
    return {
      tableData: [],
      tableHeader:[],
      tableLoading:true,
      option:{showOperate:false},
      searchParam: {
        sampleCode:"",
        userName:"",
        sampleNum:"",
        startTime:"",
        endTime:"",
        materialName:"",
        materialGradeId:"",
        checkStatus:"",
        materialCode:"",
        locationId:"",
        sampleTypeId:"",
        materialTypeId:"",
      },
      backCount:0,
      detailData:{},
      selectChoice:[],
    }
  },
  mounted() {

  },
  methods: {

    searchList(params){
      for(let key in this.searchParam){
        this.searchParam[key] = params[key]
      }
      this.getList(0,20);
    },

    getSelection(val){
      this.selectChoice = val;
    },

    generateRecord(data){
      this.$emit('generateRecord',data);
    },

    sameContrast() {
      let choiceIds = [];
      if (this.selectChoice.length == 0) {
        this.$notify({
          title: '提示',
          message: "请选择对比项！",
          type: 'warning'
        });
        return;
      }
      if (this.selectChoice.length == 1) {
        this.$notify({
          title: '提示',
          message: "请至少选择两项对比！",
          type: 'warning'
        });
        return;
      }
      let materialName = "";
      for (let data of this.selectChoice) {
        if (!materialName) {
          materialName = data.materialName;
        }
        if (materialName != data.materialName) {
          this.$notify({
            title: '提示',
            message: "请选择相同样品名称对比！",
            type: 'warning'
          });
          return;
        }
        choiceIds.push(data.sampleCode);
      }
      this.$eventBus.$emit("openSameContrast",materialName,this.searchParam,choiceIds);
    },

    handleEditData(rowData){
      this.detailData = Object.assign({},rowData);
      this.detailData.materialTypeId = this.detailData.materialType;
      this.detailData.materialGradeId = this.detailData.materialGrade;
      this.$eventBus.$emit("openSendSee",this.detailData);
    },

    getList(startIndex,pageRow) {
      let self = this;
      self.tableLoading = true;
      let searchParam = Object.assign({},this.searchParam);
      if(searchParam.endTime){
        searchParam.endTime = searchParam.endTime+" 23:59:59";
      }
      let start = typeof startIndex =="number"?startIndex:0;
      let total = typeof pageRow == "number"?pageRow:15;
      searchParam.startIndex = start;
      searchParam.totalPage = total;
      self.$http({
        url: "/drug/sample/querySampleList",
        method: "post",
        params:searchParam
      }).then(resp => {
        if (resp.success) {
          self.tableLoading = false;
          self.tableData = resp.result;
          self.backCount = self.tableData.length > 0?self.tableData[0].count:0;
          self.tableHeader =  [
            {"columnName": "sampleCode", "coloumNameCn": "检验单号"},
            {"columnName": "userName", "coloumNameCn": "申请人","width":"70px"},
            {"columnName": "createTimeSecondFt", "coloumNameCn": "送检时间"},
            {"columnName": "materialName", "coloumNameCn": "样品名称"},
            {"columnName": "sampleNum", "coloumNameCn": "样品批号"},
            {"columnName": "materialType", "coloumNameCn": "样品规格"},
            {"columnName": "materialGrade", "coloumNameCn": "样品等级"},
            {"columnName": "sampleTypeName", "coloumNameCn": "样品规模"},
            {"columnName": "locationName", "coloumNameCn": "送样地点"},
            {"columnName": "checkStatusCn", "coloumNameCn": "流程状态"}];
          self.option.showOperate = true;
        }
      });
    }
  },
}
</script>


<style lang="scss" scoped>
.el-form-no {
  text-align: right;
  font-size: 18px;
  color: #cb0000;
}

.el-dialog .el-button--primary {
  background-color: #2e827f;
  border-color: #2e827f;
    border-radius: 3px;
    padding: 5px 10px;
}

</style>
